<?php
/*
 * @Author: 李明
 * @Date: 2021-11-06 17:47:10
 * @LastEditTime: 2022-01-21 17:29:29
 * @LastEditors: 李明
 * @Description:
 * @FilePath: /CRM2.0-PHP/app/Http/Resources/User.php
 * @山东壹零贰肆信息科技有限公司
 */

namespace App\Http\Resources;

use App\Http\Controllers\common\CommonFun;
use Illuminate\Http\Resources\Json\JsonResource;

class User extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        $user = parent::toArray($request);
        // $user['name'] = $this->name;
        // $user['contact_manner'] = $this->contact_manner;
        $user['address'] = $this->address;
        $user['email'] = $this->email;
        $user['name'] = $this->name;
        $birthday = $this->birthday;
        if ($birthday) {
            $user['birthday'] = date('Y-m-d', strtotime($birthday));
        }
        $sex = $this->sex ? $this->sex : 0;
        $user['sex_name'] =  $sex == '0' ? '未知' : ($sex == '1' ? '男' : '女');
        $user['age'] = $this->birthday ? CommonFun::birthday($this->birthday) : '';
        $id_type = $this->id_type;
        $user['id_type_name'] =  $id_type == '1' ? '身份证' : '其他';
        $bank_card_count = count($this->bank_cards);
        if ($bank_card_count == 1) {
            $bank_card = $this->bank_cards->first();
        } elseif ($bank_card_count > 1) {
            $bank_card = $this->bank_cards->where('is_default', '1')->first();
        } else {
            $bank_card = false;
        }
        $user['bank_card_id'] =  $bank_card ? $bank_card->id : '';
        $user['bank_num'] =  $bank_card ? $bank_card->bank_num : '';
        $user['bank_sum_name'] =  $bank_card ? $bank_card->bank_sum_name : '';
        $user['account_name'] =  $bank_card ? $bank_card->account_name : '';
        $user['bank_name'] =  $bank_card->bank->bank_name ?? '';
        $user['bank_id'] =  $bank_card->bank->id ?? '';
        $user['worker_id'] =  $this->worker->id ?? '';
        $user['worker_name'] =  $this->worker->name ?? '';
        $user['branch_id'] =  $this->branch->id ?? '';
        $user['branch_name'] =  $this->branch->name ?? '';
        $user['pro_code'] =  $this->pro->area_code ?? '';
        $user['city_code'] =  $this->city->area_code ?? '';
        $user['area_code'] =  $this->area->area_code ?? '';
        $user['pro_name'] =  $this->pro->area_name ?? '';
        $user['city_name'] =  $this->city->area_name ?? '';
        $user['district_name'] =  $this->area->area_name ?? '';
        $user['tag_list'] =  $this->tags;
        $orders = $this->orders->whereIn('order_status', [3, 4, 6, 8])->all();
        $user['order_num'] = count($orders);
        $user['orders'] = $orders;
        $user['is_edit'] = count($orders) > 0 ? 2 : 1;
        $user['images'] = $this->images;
        $user['id_obverse_images'] = $this->images->where('img_type', '1')->all();
        $user['id_verso_images'] = $this->images->where('img_type', '2')->all();
        return $user;
    }
}
